/* used to add a friend in a ajax away
see : forum/user_page.html for usage reference */
function add_friend(request_url, html_field){
    var prc_label = new Element("span",{ 'id':'prc_label' });
    prc_label.set('text', 'processing...');
    
    var ajax_messaging = $('ajax_messaging');
    
    var response_codes = {
        "0":"Can not add self as friend",
        "1":"Can not remove self as friend",
        "2":"User does not exist",
        "3":"New friend added",
        "4":"User removed from friend list",
        "5":"Log yourself in the system first"
    }
    var morph = new Fx.Morph( "ajax_messaging" );
    
    var friend_request = new Request(
                                     {
                                        url:request_url,
                                        onRequest: function (el){ prc_label.inject(document.body); },
                                        onSuccess:function(txt,xml){
                                            /* making sure the ajax_messaging can not be seen */
                                            ajax_messaging.setStyle('opacity',0);
                                            ajax_messaging.setStyle('height',0);
                                            ajax_messaging.set('text',response_codes[txt]);
                                            var font_size = ajax_messaging.getStyle('font-size').split('px')[0].toInt();
                                            
                                            morph.chain( function(){
                                                var hide_messaging = function(){
                                                    morph.start({
                                                        'opacity':0,
                                                        'height':0,
                                                        'margin-bottom':0,
                                                        'padding':0
                                                        });
                                                };
                                                /* ajax_messaging should disappear after 3 seconds */
                                                hide_messaging.delay(3000, ajax_messaging);
                                                
                                                /* erasing upper label */
                                                prc_label.destroy();
                                            });
                                            
                                            morph.start({
                                                'margin-bottom':5,
                                                'height': font_size+10,
                                                'opacity':1,
                                                'padding':5
                                            });
                                            
                                            if(txt=="4"){
                                                html_field = $(html_field);
                                                var field_morph = new Fx.Morph(html_field);
                                                field_morph.chain(function(){
                                                    html_field.destroy();
                                                });
                                                
                                                field_morph.start({
                                                    'opacity':0,
                                                    'height':0,
                                                    'margin':0,
                                                    'padding':0,
                                                    'border-width':0
                                                });
                                            }
                                            
                                        },
                                        onFailure:function(el){ alert("Could not connect to server"); prc_label.destroy(); }
                                    });
    friend_request.send();
} /* add_friend end */

/* clear all messages of success after 3 seconds */
function clear_sucess(){
    $each($$(".success"), function(el, index){        
        var morph_fn = function(){
            new Fx.Morph(this).start({
                'opacity':0,
                'height':0,
                'margin':0,
                'padding':0,
                'border-width':0
            });
        };
        morph_fn.delay(3000, el);
    });
} /* clear_sucess end */

function load_pvt(request_url, answer_box){
    var answer_box = $(answer_box);
    var request = new Request(
        {
            url:request_url,
            onRequest:function(el){
                answer_box.addClass("active");
            },
            onSuccess:function(txt, xml){
                var json_answer = JSON.decode(txt);
                if (json_answer.status=="ok"){
                    var forum_message = json_answer.forum_message;
                    
                    if(answer_box.getChildren().length==0){
                        var label_div = new Element( "div", {"id":"pvt_box_label","class":"clear"} );
                        var label_span = new Element("span", {"id":"pvt_label_span","class":"clear"});
                        var content_div = new Element("div", {"id":"pvt_box_content","class":"clear"});
                        
                        label_span.inject(label_div);
                        label_div.inject(answer_box);
                        content_div.inject(answer_box);
                    } else {
                        var label_div = $("pvt_box_label");
                        var label_span = $("pvt_label_span");
                        var content_div = $("pvt_box_content");
                    }
                    
                    label_span.set('text', forum_message.title);
                    content_div.set("text", forum_message.content);
                    
                } else if (json_answer.status=="error"){
                    var error_div = new Element( "div", {"class":"clear error"} );
                    error_div.set("text", json_answer.message);
                    error_div.inject( answer_box );
                } else{
                    alert("CODING ERROR ");
                }
            },
            onFailure:function(el){alert("SERVER ERROR");}
        } );
    request.send();
}

function you_sure(txt){
    return confirm(txt);
}